function Molecule(formula, common_name, atoms, quantity) {
  this.formula = formula;
  this.common_name = common_name != null ? common_name : f2m[this.formula].common_name;
  this.atoms = atoms != null ? atoms : f2m[this.formula].atoms;
  this.quantity = quantity != null ? quantity : 0;
}

Molecule.prototype = {
  drawSpaceFillingModel : function(ctx, x, y, scale) {
    ctx.clearRect(x - scale, y - scale, 2 * scale, 2 * scale);
    var sfm = f2sfm[this.formula];
    for (var symbol in sfm) { // e == element shortname
      var aca = sfm[symbol]; // atom coordinates array;
      for (var j in aca) {
        var ac = aca[j];
        var a = new Atom(symbol);
        a.drawAtom(ctx, x + 0.64 * ac[0] * scale, y + 0.64 * ac[1] * scale, scale);
      }
    }
  },
  drawConstructModel : function(ctx, x, y, scale, size) {
    var atoms = construct_score.atoms;
    ctx.clearRect(0, 0, ctx.canvas.width, ctx.canvas.height);
    if (atoms.length == 0) {
      if (size == 2) {
        new Atom("?").drawAtom(ctx, x + 0.38 * scale, y, scale);
        new Atom("?").drawAtom(ctx, x - 0.38 * scale, y, scale);
      }
    } else {
      if (size == 2) {
        var a1, a2;
        a1 = new Atom(atoms[0] ? atoms[0] : "?");
        a2 = new Atom(atoms[1] ? atoms[1] : "?");
        a1.x_offset = (0.38 + (a1.symbol != "?" ? 0.14 * (s2e[a1.symbol].period - 2) : 0)) * scale;
        a2.x_offset = (0.38 + (a2.symbol != "?" ? 0.14 * (s2e[a2.symbol].period - 2) : 0)) * scale;
        a2.drawAtom(ctx, x + a2.x_offset, y, scale);
        a1.drawAtom(ctx, x - a1.x_offset, y, scale);
      }
    }
  }
};

var f2m = {  // short name to molecule map
  "O2"   : new Molecule("O2", "Oxygen", { O : 2 }, null),
  "CO"   : new Molecule("CO", "Carbon Monoxide", { C : 1, O : 1 }, null),
  "NO"   : new Molecule("NO", "Nitric Oxide", { N : 1, O : 1 }, null),
  "HCl"  : new Molecule("HCl", "Hydrochloric Acid", { H : 1, Cl : 1 }, null),
  "NaCl" : new Molecule("NaCl", "Sodium Chloride", { Na : 1, Cl : 1 }, null),
  "CO2"  : new Molecule("CO2",  "Carbon Dioxide", { C : 1, O : 2 }, null),
  "CH4"  : new Molecule("CH4",  "Methane", { C : 1, H : 4 }, null),
  "OH-"  : new Molecule("OH-",  "Hydroxide Ion", { H : 1, O : 1}, null ),
  "H2O"  : new Molecule("H2O",  "Water", { H : 2, O : 1 }, null),
  "H3O+" : new Molecule("H3O+", "Hydronium Ion", { H: 3, O : 1 }, null),
  "H2O2" : new Molecule("H2O2", "Hydrogen Peroxide", { H : 2, O : 2 }, null),
  "HCOOH" : new Molecule("HCOOH", "Formic Acid", { H : 2, C : 1, O : 2 }, null),
  "HCOONa" : new Molecule("HCOONa", "Sodium Formate", { H : 1, C: 1, O : 2, Na : 1 }, null),
  "NaHCO3" : new Molecule("NaHCO3", "Baking Soda", { Na : 1, H : 1, C : 1, O : 3 }, null),
  "CH3COOH" : new Molecule("CH3COOH", "Acetic Acid", { C: 2, H  : 4, O : 2 }, null),
  "CH3COONa" : new Molecule("CH3COONa", "Sodium Acetate", { C : 2, H : 3, O : 2, Na : 1 }, null)
};

var f2sfm = { // short name to simple model
  "O2"   : { O : [ [-0.5, 0.0], [0.5, 0.0] ] },
  "CO"   : { C : [ [-0.5, 0.0] ], O : [ [0.5, 0.0] ] },
  "NO"   : { N : [ [-0.5, 0.0] ], O : [ [0.5, 0.0] ] },
  "HCl"  : { H : [ [-0.7, 0.0] ], Cl : [ [0.5, 0.0] ] },
  "NaCl" : { Na : [ [-0.5, 0.0] ], Cl : [ [0.5, 0.0]] },
  "CO2"  : { O : [ [-1.0, 0.0], [1.0, 0.0] ], C : [ [0.0, 0.0] ] },
  "CH4"  : { H : [ [0.0, 1.0], [1.0, 0.0], [0.0, -1.0], [-1.0, 0.0] ], C : [ [0.0, 0.0] ] },
  "OH-"  : { H : [ [0.8, -0.3] ], O: [ [0.0, 0.3] ] },
  "H2O"  : { H : [ [-0.8, -0.3], [0.8, -0.3] ], O : [ [0.0, 0.3] ] },
  "H3O+" : { H : [ [-0.8, -0.6], [0.8, -0.6], [0.0, 1.0] ], O : [ [0.0, 0.0] ] },
  "H2O2" : { H : [ [-1.3, 0.6], [1.3, -0.6] ], O : [ [-0.5, 0.0], [0.5, 0.0] ] },
  "HCOOH" : { H : [ [-0.7, -0.6], [1.8, -0.7] ], O : [ [0.8, -0.7], [0.0, 1.0] ], C : [ [0.0, 0.0] ] },
  "HCOONa" : { H : [ [-0.7, -0.6] ], Na : [ [1.9, -0.7] ], O : [ [0.8, -0.7], [0.0, 1.0] ], C : [ [0.0, 0.0] ] },
  "NaHCO3" : { H : [ [-1.8, -0.7] ], Na : [ [1.9, -0.7] ], O : [ [-0.8, -0.7], [0.8, -0.7], [0.0, 1.0] ], C : [ [0.0, 0.0] ] },
  "CH3COOH" : { H : [ [-1.8, 0.0], [-0.8, 1.0], [-0.8, -1.0], [1.9, -0.7] ], O : [ [0.9, 0.7], [0.9, -0.7] ], C: [ [-0.8, 0.0], [0.2, 0.0] ] },
  "CH3COONa" : { H : [ [-1.8, 0.0], [-0.8, 1.0], [-0.8, -1.0] ], Na : [ [2.0, -0.7] ], O : [ [0.9, 0.7], [0.9, -0.7] ], C: [ [-0.8, 0.0], [0.2, 0.0] ] }
};

$(document).ready(validateMolecules);

// checks all molecules have proper simple models
function validateMolecules() {
  for (var sn in f2m) {
    var m = f2m[sn];
    var sm = f2sfm[sn];
    for (var e in m.atoms) {
      if (m.atoms[e] != sm[e].length) {
        alert("Error in molecular formula and/or simple model of " + sn);
      }
    }
  }
}
